<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 文章管理
 */
namespace app\home\model;

use app\home\model\Model;
// use ms\core\Page;

class Article extends Model{

	private $article = 'sys_article';
    private $articleclass = 'sys_articleclass';
    
    
    /**
     * 首页内容列表
     * @param array $articlelist
     * @param array $where
     */
    public function index_article($where='', $lang='default',$page=1,$page_size=10)
    {
        $articlelist = Model::name($this->article)
            ->field('*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
        foreach ($articlelist as $key => $value) {
            if($value['is_open']>0){
                $articlelist[$key]['url']=$value['open_url'];
            }
            if(!empty($value['introduction'])){
                $articlelist[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['titles'])){
                $articlelist[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($articlelist[$key]['titles']);
        }
        return $articlelist;
    }

    /**
     * 内容列表
     * @param array $articlelist
     * @param array $where
     */
    public function articlelist($where='', $lang='default',$page=1,$page_size=10)
    {
        $articlelist['datalist'] = Model::name($this->article)
            ->field('open_url,introduction,titles,title,is_open,id,cid,url')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
        foreach ($articlelist['datalist'] as $key => $value) {
            if($value['is_open']>0){
                $articlelist['datalist'][$key]['url']=$value['open_url'];
            }
            if(!empty($value['introduction'])){
                $articlelist['datalist'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['titles'])){
                $articlelist['datalist'][$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($articlelist['datalist'][$key]['titles']);
        }
         //数据总数
        $articlelist['total']=Model::name($this->article)
            ->where($where)
            ->count('id');
        return $articlelist;
    }

    /**
     * 内容詳情
     * @param array $article 
     * @param array $where 
     */ 
    public function article($where='',$lang='default',$file='*')
    {
        $article = Model::name($this->article)
            ->field($file)
            ->where($where)
            ->find();
        if(!empty($article['introduction'])){
            $article['introduction']=isset(json_decode($article['introduction'],true)[$lang])?json_decode($article['introduction'],true)[$lang]:'';  
        }
        if(!empty($article['titles'])){
            $article['title']=isset(json_decode($article['titles'],true)[$lang])?json_decode($article['titles'],true)[$lang]:$article['title'];
        }
        if(!empty($article['content'])){
            $article['content']=isset(json_decode($article['content'],true)[$lang])?htmlspecialchars_decode(json_decode($article['content'],true)[$lang]):'';
        }
        unset($article['titles']);    
        return $article;
    }


    /**
     * 获取内容類別
     * @param array $getcateinfo 
     * @param array $where 
     */ 
    public function getcateinfo($where='', $lang='default', $file='*')
    {
        $cateinfo = Model::name($this->articleclass)
            ->field($file)
            ->where($where)
            ->find();
        if(!empty($cateinfo['introduction'])){
            $cateinfo['introduction']=isset(json_decode($cateinfo['introduction'],true)[$lang])?json_decode($cateinfo['introduction'],true)[$lang]:'';  
        }
        if(!empty($cateinfo['titles'])){
            $cateinfo['title']=isset(json_decode($cateinfo['titles'],true)[$lang])?json_decode($cateinfo['titles'],true)[$lang]:$cateinfo['title'];
        }
        return $cateinfo;
    }

    /**
     * 获取内容類別
     * @param array $getcateinfo 
     * @param array $where 
     */ 
    public function getcatelist($where='', $lang='default', $file='*')
    {
        $cateinfo = Model::name($this->articleclass)
            ->field($file)
            ->where($where)
            ->select()
            ->toArray();
        foreach ($cateinfo as $key => $value) {
            if(!empty($value['introduction'])){
                $cateinfo[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['titles'])){
                $cateinfo[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
        }
        
        return $cateinfo;
    }



}
